<template>
  <div>
    <el-result v-if="callback_type" icon="success" title="成功" :subTitle="callback_type">
      <template slot="extra">
        <el-button size="mini" type="primary" @click="toIndex">返回</el-button>
      </template>
    </el-result>
  </div>
</template>

<script>
import {cert_callback, profile, signed_callback, verify_create_user} from "@/request/api";

export default {
  name: "CallBack",
  data(){
    return{
      callback_type:null
    }
  },
  methods:{
    //用户池实名认证后添加档案
    verifyCreateUser(){
        verify_create_user().then(res =>{
          console.log('res',res);
        }).catch(err =>{
          console.log(err.errors);
          if (!err.errors.includes('用户池中不存在card_id')) {
            //弹出异常，这里的异常需要醒目 持久展示
            this.$alert(err.msg, err.errors, {
              confirmButtonText: '确定',
            });
          }
        })
    },
    toIndex(){
      this.$router.push('/user')
    },
    async sign(){
      let query = this.$route.query;
      if (query.transactionNo && query.sign) {
        try {
          const res = await cert_callback(query)
          this.callback_type = '实名认证成功'
          this.verifyCreateUser();
        } catch (e) {
          console.log('实名认证回调错误:', e.response.data)
        }

      }
      //证明是签署合同回调
      else if (query.transaction_id) {
        const timestamp = query.timestamp || query.viewpdf_url.match(/timestamp=(\d+)&/)[1]
        const msg_digest = query.msg_digest || query.viewpdf_url.match(/msg_digest=(\S+?)&/)[1]
        const data = {timestamp, msg_digest, transaction_id: query.transaction_id}
        try {
          const res = await signed_callback(data)
          this.callback_type = '报名确认书签署成功'
        } catch (e) {
          console.log('签署认证回调错误:', e.response.data)
        }
      } else {
        alert('没有任何回调！')
      }
    }
  },
  mounted() {
    this.sign();
  }
  // setup() {
  //   const callback_type = ref(null)
  //   onMounted(async () => {
  //     const query = useRoute().query
  //     //证明是实名认证的回调
  //     if (query.transactionNo && query.sign) {
  //       try {
  //         const res = await cert_callback(query)
  //         callback_type.value = '实名认证成功'
  //       } catch (e) {
  //         console.log('实名认证回调错误:', e.response.data)
  //       }
  //
  //     }
  //     //证明是签署合同回调
  //     else if (query.transaction_id) {
  //       const timestamp = query.viewpdf_url.match(/timestamp=(\d+)&/)[1]
  //       const msg_digest = query.viewpdf_url.match(/msg_digest=(\S+)&/)[1]
  //       const data = {timestamp, msg_digest, transaction_id: query.transaction_id}
  //       try {
  //         const res = await signed_callback(data)
  //         callback_type.value = '报名确认书签署成功'
  //       } catch (e) {
  //         console.log('签署认证回调错误:', e.response.data)
  //       }
  //     } else {
  //       alert('没有任何回调！')
  //     }
  //   })
  //   return {callback_type}
  // }
}

</script>
<style scoped>
.el-result{
  margin-top: 80px;
}
</style>
